package cn.harvest.storage.bo.admin;

import java.util.List;

import cn.harvest.storage.dao.DAO;
import cn.harvest.storage.po.Employee;

public class EmployeeService {

	private DAO dao = DAO.getDAO();

	private EmployeeService() {

	}

	public static EmployeeService getService() {

		EmployeeService service = new EmployeeService();
		return service;
	}

	public boolean create(Employee emp) {

		if (1 == emp.getIsOper()) {
			emp.setOperatorName(emp.getEmployeeCid());
			emp.setOperatorPass("0000");
		}

		return dao.create(emp);
	}
	
	public boolean update(Employee emp) {
		
		if (1 == emp.getIsOper()) {
			if (null == emp.getOperatorName()) {
				emp.setOperatorName(emp.getEmployeeCid());
				emp.setOperatorPass("0000");
			}
		} else {
			if (null != emp.getOperatorName()) {
				emp.setOperatorName(null);
				emp.setOperatorPass(null);
			}
		}
		
		return dao.update(emp);
	}

	public List selectBySQL(Employee emp) {

		StringBuffer sql = new StringBuffer();
		
		sql.append(" SELECT e.employee_id, e.employee_name, e.employee_cid, eem.employment_name, e.employee_state, e.employee_tel, e.employee_add, e.employment_id, e.isOper, e.employee_memo ");
		sql.append(" FROM employee e, employment eem ");
		sql.append(" WHERE 1 = 1 ");
		sql.append(" AND eem.employment_id = e.employment_id ");
		if (!(null == emp.getEmployeeId() || "".equals(emp.getEmployeeId()))) {
			sql.append(" AND e.employee_id = '" + emp.getEmployeeId() + "' ");
		}
		if (!(null == emp.getEmployeeCid() || "".equals(emp.getEmployeeCid()))) {
			sql.append(" AND e.employee_cid = '" + emp.getEmployeeCid() + "' ");
		}
		if (!(null == emp.getEmployeeName() || "".equals(emp.getEmployeeName()))) {
			sql.append(" AND e.employee_name LIKE '%" + emp.getEmployeeName() + "%' ");
		}
		if (!(null == emp.getEmployment() || "".equals(emp.getEmployment().getEmploymentId()))) {
			sql.append(" AND e.employment_id = '" + emp.getEmployment().getEmploymentId() + "' ");
		}
		if (!(null == emp.getEmployeeState() || "".equals(emp.getEmployeeState()))) {
			sql.append(" AND e.employee_state = '" + emp.getEmployeeState() + "' ");
		}
		sql.append(" ORDER BY e.employee_id ");

		System.out.println("### - EmployeeService -> selectBySQL -> #SQL \r\n	 " + sql);
		
		return dao.selectBySQL(sql.toString());
	}
	
	public Employee selectByHQL(String empId) {
		StringBuffer hql = new StringBuffer();
		hql.append(" FROM Employee emp WHERE emp.employeeId = " + empId);
		
		System.out.println("### - EmployeeService -> selectByHQL -> #HQL \r\n	 " + hql);
		
		Employee emp = (Employee) dao.selectByHQL(hql.toString()).get(0);
		return emp;
	}
	
	public static void main(String[] args) {

	}
}
